/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    typedef struct ListNode* Node;
    ListNode* mergeKLists(vector<ListNode*>& lists) {
        priority_queue<int,vector<int>,greater<int>> hp;
        Node virnode=new ListNode(-1);
        Node head=virnode;
        for(int i=0;i<lists.size();i++)
        {
            Node q=lists[i];
            while(q)
            {
                hp.push(q->val);
                q=q->next;
            }
        }
        int n=hp.size();
        for(int i=0;i<n;i++)
        {
            virnode->next=new ListNode(hp.top());
            virnode=virnode->next;
            hp.pop();
        }
        return head->next;
    }
};
